package com.api.gbhg.liuwenbo.version2.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.version2.pojo.BudgetSectionIncome2;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

/**
 * @Description 事业合计
 * @Author lwb
 * @Date 2022.7.21
 */
@Path("gb/bb3")
public class CauseApi extends BudgetSectionIncomeApi2{

    @POST
    @Path("/getCauseList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getCauseList(Map param){
        Map msgMap = new HashMap();
        if (param == null ||!param.containsKey("nf") ||!param.containsKey("jz")||!param.containsKey("dw")){
            msgMap.put("code","500");
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String, Object> paramFormat = paramFormat(param);

        List<BudgetSectionIncome2> Data ;//数据集合

        try {
            Data = getCzbksrData(paramFormat);
        }catch (Exception e){
            msgMap.put("code","500");
            msgMap.put("msg","程序异常，请联系管理员！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("1",Data);

        msgMap.put("code","200");
        msgMap.put("msg","");
        msgMap.put("data",map);
        return JSON.toJSONString(msgMap);
    }

    //获取事业单位
    public static String getdw(){
        RecordSet rs = new RecordSet();
        String sql = "select dlhsdw as 'dwid' from uf_dlhsdwgl left join uf_dwsxpz " +
                "on uf_dlhsdwgl.dlhsdw=uf_dwsxpz.xzxtdw where dwxz=1 ";
        rs.executeQuery(sql);
        List<String> list = new ArrayList<>();
        while (rs.next()){
            list.add(rs.getString("dwid"));
        }
        return String.join(",",list);
    }

    public static String getdw(String dw){
        RecordSet rs = new RecordSet();
        String sql = "select dlhsdw as 'dwid' from uf_dlhsdwgl left join uf_dwsxpz " +
                "on uf_dlhsdwgl.dlhsdw=uf_dwsxpz.xzxtdw where dwxz=1 ";
        rs.executeQuery(sql);
        while (rs.next()){
            if (rs.getString("dwid").equals(dw))
                return dw;
        }
        return "";
    }

    @Override
    public List<BudgetSectionIncome2> getCzbksrData(Map<String, Object> param) {
        List<BudgetSectionIncome2> listBean = new ArrayList<>();

        String nf = param.get("nf").toString();
        String jz = param.get("jz").toString();
        String paramDW = param.get("dw").toString();

        String sql = "select id,gnkmmc,kmdm,"+jz+",yjsr,snjzhpje,yhpje,yqndyjjzje,yjsr,dw from uf_zyczbksr_km where srnf = "+nf+" and dw in ("+paramDW+") ";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        while (rs.next()){
            BudgetSectionIncome2 bean = new BudgetSectionIncome2();
            bean.setId(rs.getString("id"));
            bean.setDataid(rs.getString("id"));
            bean.setKmmc(rs.getString("kmdm"));
            bean.setKmjxj(0);
            bean.setSnjz(rs.getString(jz));
            bean.setBnsr(rs.getString("yjsr"));
            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jz)).add(new BigDecimal(rs.getString("yjsr")))));

            String dw = rs.getString("dw");

            List<BudgetSectionIncome2> xjList = getCzbksrEjData(rs.getString("id"), jz,dw,nf);
            if (xjList.size()>0){
                BigDecimal snjzysap = BigDecimal.ZERO;//上年结转预算安排
                BigDecimal bnsrysap = BigDecimal.ZERO;//本年收入预算安排
                BigDecimal snjzwap = BigDecimal.ZERO; //上年结转未安排
                BigDecimal bnsrwap = BigDecimal.ZERO; //本年收入未安排
                BigDecimal snjzyszc = BigDecimal.ZERO; //上年结转预算支出
                BigDecimal bnsryszc = BigDecimal.ZERO; //本年收入预算支出
                BigDecimal snjzyszxl = BigDecimal.ZERO; //上年结转预算执行率
                BigDecimal bnsryszxl = BigDecimal.ZERO; //本年收入预算执行率
                for (BudgetSectionIncome2 beans:xjList){
                    snjzysap = snjzysap.add(new BigDecimal(beans.getSnjzysap()));
                    bnsrysap = bnsrysap.add(new BigDecimal(beans.getBnsrysap()));

                    snjzwap = snjzwap.add(new BigDecimal(beans.getSnjzwap()));
                    bnsrwap = bnsrwap.add(new BigDecimal(beans.getBnsrwap()));

                    snjzyszc = snjzyszc.add(new BigDecimal(beans.getSnjzyszc()));
                    bnsryszc = bnsryszc.add(new BigDecimal(beans.getBnsryszc()));

                    snjzyszxl = snjzyszxl.add(new BigDecimal(beans.getSnjzyszxl()));
                    bnsryszxl = bnsryszxl.add(new BigDecimal(beans.getBnsryszxl()));
                }
                bean.setSnjzysap(snjzysap.toString());
                bean.setBnsrysap(bnsrysap.toString());
                bean.setHj1(snjzysap.add(bnsrysap).toString());

                bean.setSnjzwap(snjzwap.toString());
                bean.setBnsrwap(bnsrwap.toString());
                bean.setHj2(snjzwap.add(bnsrwap).toString());

                bean.setSnjzyszc(snjzyszc.toString());
                bean.setBnsryszc(bnsryszc.toString());
                bean.setHj3(snjzyszc.add(bnsryszc).toString());

                bean.setSnjzyszxl(snjzyszxl.toString());
                bean.setBnsryszxl(bnsryszxl.toString());
                bean.setHj4(snjzyszxl.add(bnsryszxl).toString());

                bean.setXj(xjList);
            }else{
                //已到最下级  取自身值
                bean.setSnjzysap(rs.getString("snjzhpje"));
                bean.setBnsrysap(rs.getString("yhpje"));
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));

                bean.setSnjzwap(rs.getString("yqndyjjzje"));
                bean.setBnsrwap(rs.getString("yjsr"));
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

                String snbxje = getDfcz_tybxje("0", "1", "0", rs.getString("id"), dw, nf);
                bean.setSnjzyszc(snbxje);
                String bnbxje = getDfcz_tybxje("0", "0", "0", rs.getString("id"), dw, nf);
                bean.setBnsryszc(bnbxje);
                bean.setHj3(String.valueOf(new BigDecimal(bean.getSnjzyszc()).add(new BigDecimal(bean.getBnsryszc()))));

                BigDecimal snzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getSnjzysap()).compareTo(new BigDecimal("0"))>0)
                    snzxl = new BigDecimal(bean.getSnjzyszc()).divide(new BigDecimal(bean.getSnjzysap()), 2, BigDecimal.ROUND_HALF_UP);
                bean.setSnjzyszxl(snzxl.toString());

                BigDecimal bnzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getBnsrysap()).compareTo(new BigDecimal("0"))>0)
                    bnzxl = new BigDecimal(bean.getBnsryszc()).divide(new BigDecimal(bean.getBnsrysap()), 2, BigDecimal.ROUND_HALF_UP);
                bean.setBnsryszxl(bnzxl.toString());
                bean.setHj4(String.valueOf(snzxl.add(bnzxl)));
            }

            bean.setName(rs.getString("gnkmmc"));
            listBean.add(bean);
        }


        return AdjustDataStructure(new ArrayList<>(),listBean);
    }

    public static List<BudgetSectionIncome2> AdjustDataStructure(List<BudgetSectionIncome2> list,List<BudgetSectionIncome2> listBean){
        for (int i = 0; i < listBean.size(); i++) {
            if (list.size() == 0){
                list.add(listBean.get(i));
                continue;
            }

            boolean success = false;
            for (int j = 0; j < list.size(); j++) {
                if (listBean.get(i).equals(list.get(j))){
                    sumList(list,j,listBean.get(i));
                    success = true;
                    break;
                }
            }

            if (success == false){
                list.add(listBean.get(i));
            }
        }

        return list;
    }

    public static void sumList(List<BudgetSectionIncome2> targetList,int ix,BudgetSectionIncome2 ds){
        BudgetSectionIncome2 bi = targetList.get(ix);
        if (bi.getId().equals("46"))
            System.out.println("99");
        if (ds.getDataid()!=null && !ds.getDataid().isEmpty() && bi.getDataid()!=null && !bi.getDataid().isEmpty()) {
            bi.addDataid(ds.getDataid());
        }else if(ds.getDataid()!=null && !ds.getDataid().isEmpty() && (bi.getDataid() == null || bi.getDataid().isEmpty())){
            bi.setDataid(ds.getDataid());
        }
        bi.addSnjz(ds.getSnjz());
        bi.addBnsr(ds.getBnsr());
        bi.addHj(ds.getHj());
        bi.addSnjzysap(ds.getSnjzysap());
        bi.addBnsrysap(ds.getBnsrysap());
        bi.addHj1(ds.getHj1());
        bi.addSnjzwap(ds.getSnjzwap());
        bi.addBnsrwap(ds.getBnsrwap());
        bi.addHj2(ds.getHj2());
        bi.addSnjzyszc(ds.getSnjzyszc());
        bi.addBnsryszc(ds.getBnsryszc());
        bi.addHj3(ds.getHj3());
        bi.addSnjzyszxl(ds.getSnjzyszxl());
        bi.addhj4(ds.getHj4());

        if (ds.getXj() != null && ds.getXj().size()>0){
            if (bi.getXj() != null && bi.getXj().size()>0){

//                List<BudgetSectionIncome2> xjList = bi.getXj();
//                List<BudgetSectionIncome2> dsXj = ds.getXj();
//
//                List<BudgetSectionIncome2> newList = ds.getXj();
//                for (int i = 0; i < dsXj.size(); i++) {
//
//                    for (int j = 0; j < xjList.size(); j++) {
//                        if (xjList.get(j).equals(dsXj.get(j))){
//                            sumList(xjList,i,dsXj.get(j));
//                        }
//                    }
//                }

                List<BudgetSectionIncome2> list = bi.getXj();
                List<BudgetSectionIncome2> listBean = ds.getXj();

                AdjustDataStructure(list,listBean);

            }else{
                bi.setXj(ds.getXj());
            }

        }

    }

    /**
     * 参数转换
     * @param param
     * @return
     */
    @Override
    public Map<String,Object> paramFormat(Map<String,Object> param){
        Integer nf = param.get("nf").equals("")? LocalDate.now().getYear():Integer.parseInt(param.get("nf").toString());
        String jz = param.get("jz").equals("")?"yqndyjjzje":param.get("jz").toString();
        String dw = param.get("dw").equals("")?getdw(): getdw(param.get("dw").toString());

        param.put("nf",nf);
        param.put("jz",jz);
        param.put("dw",dw);
        return param;
    }
}
